import { SUCCESS_CODE, ERROR_CODE, VALIDATE_FAILED_CODE } from '../server.config';
// Error：权限异常
class AuthError extends Error {
	constructor(message) {
		super(message);
		this.name = 'AuthError';
	}
}
// Error: 目标数据不存在
class TargetDataNoneError extends Error {
	constructor(message) {
		super(message);
		this.name = 'TargetDataNoneError';
	}
}

class ParameterError extends Error {
	constructor(message) {
		super(message);
		this.name = 'ParameterError';
	}
}

// 创建resdata
class Result {
	constructor() {
		this.success_count = 0;
		this.error_count = 0;
	}
	success(data = {}, msg = '成功') {
		this.success_count += 1;
		return {
			code: SUCCESS_CODE,
			data,
			msg,
		};
	}
	putsuccess() {
		this.success_count += 1;
		return { code: SUCCESS_CODE, msg: '编辑成功' };
	}
	deletesuccess() {
		this.success_count += 1;
		return { code: SUCCESS_CODE, msg: '删除成功' };
	}
	success_mesage(msg = '成功') {
		this.success_count += 1;
		return {
			code: SUCCESS_CODE,
			msg,
		};
	}
	error(msg = '程序异常', code = ERROR_CODE) {
		this.error_count += 1;
		return {
			code,
			msg,
		};
	}
	validateFailed(msg) {
		if (!msg) msg = '权限异常';
		this.error_count += 1;
		const code = VALIDATE_FAILED_CODE;
		return {
			code,
			msg,
		};
	}
}

export { AuthError, Result, TargetDataNoneError, ParameterError };
